"""
Portscanner module

Scans IP for open and closed ports

"""

import sys
from scapy.all import *

class PortScan:
        """Scans an IP for Open Ports
        
        Keyword arguments:
        ipToScan -- Remote Machine IP Address
        dstPort -- List Of Ports To Scan Remote Machine
        srcPort -- Local Port
        
        """
        def __init__(self,ipToScan,dstPort,srcPort):
            """Default Constructor"""
            self.ipToScan = ipToScan
            self.dstPort = dstPort
            self.srcPort = srcPort;
                
        def setIp(self,ipToScan):
            """Sets Remote Machine IP Address
            
            keyword arguments:
            ipToScan -- Remote Machine IP Address
            """
            self.ipToScan = ipToScan
                 
        def scanports(self):
                """Scans Target"""              
                openPorts = []
                closedPorts = []
                ip = IP(dst=str(self.ipToScan))
                tcp = TCP(dport=self.dstPort,sport=self.srcPort,flags="S")
                ans,unans = sr(ip/tcp,timeout=1)
                for sent,rcvd in ans:
                        if rcvd.haslayer(TCP):
                                if (rcvd.getlayer(TCP).flags == 18):
                                        openPorts.append(sent.dport)
                                elif (rcvd.getlayer(TCP).flags == 4 | rcvd.getlayer(TCP).flags == 20):
                                        closedPorts.append(sent.dport)
                return openPorts, closedPorts

